package com.zt.mybatis.binding;

import java.lang.reflect.Proxy;
import java.util.Map;

/**
 * @author: zhangtao
 * @description: 映射器代理工厂：创建映射器代理
 * @date: 2024-12-09 15:19
 */

public class MapperProxyFactory<T> {

    /**
     * 映射器接口
     * 指定项目中的mapper文件，也就是被@Mapper注解标识的接口
     * 这里通过反射获取接口信息
     */
    private final Class<T> mapperInterface;

    public MapperProxyFactory(Class<T> mapperInterface) {
        this.mapperInterface = mapperInterface;
    }

    public T newInstance(Map<String, String> sqlSession) {
        final MapperProxy<T> mapperProxy = new MapperProxy<>(sqlSession, mapperInterface);
        return (T) Proxy.newProxyInstance(mapperInterface.getClassLoader(), new Class[]{mapperInterface}, mapperProxy);
    }
}
